package net.wanho.systemSettings.service.impl;

import net.wanho.commons.exception.DaoException;
import net.wanho.commons.exception.ServiceException;
import net.wanho.commons.factory.BeanFactory;
import net.wanho.systemSettings.dao.MenuDao;
import net.wanho.systemSettings.po.Menu;
import net.wanho.systemSettings.service.MenuService;
import net.wanho.systemSettings.vo.MenuVo;
import org.apache.commons.beanutils.BeanUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

public class MenuServiceImpl implements MenuService {
    private MenuDao menuDao = BeanFactory.getBean(MenuDao.class) ;
    @Override
    public List<MenuVo> selectMenuVoByPositionId(Integer positionId) throws ServiceException {
        //查询所有的一级菜单
        try {
            List<Menu> parentMenuList = menuDao.selectMenuByPositionIdAndParentMenuId(positionId, null);
            List<MenuVo> menuVoList = new ArrayList<>() ;
            for (Menu menu : parentMenuList) {
                MenuVo menuVo = new MenuVo();
                BeanUtils.copyProperties(menuVo,menu);
                //根据当前一级菜单的id  查询 二级菜单
                List<Menu> sonMenuList = menuDao.selectMenuByPositionIdAndParentMenuId(positionId, menu.getMenuId());
                menuVo.setChildrenMenuList(sonMenuList);
                menuVoList.add(menuVo) ;
            }
            return  menuVoList ;
        } catch (DaoException |IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
            throw new ServiceException(e.getMessage()) ;
        }
    }
}
